package com.atguigu.exam;

import java.util.HashSet;
import java.util.Iterator;

/**
 * @author xbd
 * @create 2021-05-28 11:02
 */
public class CarTest {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();

        hashSet.add(new Car("大众",66666));
        hashSet.add(new Car("奔驰",58888));
        hashSet.add(new Car("奔驰",58888));
        hashSet.add(new Car("宝马",45999));
        hashSet.add(new Car("一汽",39999));
        hashSet.add(new Car("一汽",39999));
        hashSet.add(new Car("丰田",56666));

        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}

class Car{
    private String name;
    private int price;

    public Car() {
    }

    public Car(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Car car = (Car) o;

        if (price != car.price) return false;
        return name != null ? name.equals(car.name) : car.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + price;
        return result;
    }
}
